package com.pk4;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;

/**
 * IP帮助类
 *
 * @author JinLiwei
 * @since 2020-08-15
 */
class IPHelper {
    private static final int MAX_VALUE = 255;
    private static final int MIN_VALUE = 0;
    private static final int MAX_LENGTH = 8;
    private static final String ERROR_IP = "";
    private static final int GROUP_LENGTH = 4;

    /**
     * 获取IP各组的值
     *
     * @param ip 输入ip
     * @return 各组的值
     */
    public int[] getIpGroup(String ip) {
        String[] splitIps = ip.split("\\.");
        int[] ints = Arrays.stream(splitIps).mapToInt(x -> Integer.parseInt(x)).toArray();
        return ints;
    }

    /**
     * 获取各组值的二进制表示，不足8位的前面补零
     *
     * @param ipVal 各组值
     * @return 8位二进制表示
     */
    public String getBinRepIP(int ipVal) {
        String s = Integer.toBinaryString(ipVal);
        int sLen = s.length();
        StringBuilder sb = new StringBuilder();
        while (sLen < MAX_LENGTH) {
            sb.append("0");
            sLen++;
        }
        if (sb.length() > 0) {
            return sb.toString() + s;
        } else {
            return s;
        }

    }

    /**
     * 子网掩码是否有效
     *
     * @param maskIP 子网掩码
     * @return 是否有效
     */
    public boolean isValidMaskIP(String maskIP) {
        boolean isOk = true;
        int[] ipGroup = getIpGroup(maskIP);
        if (ipGroup == null || ipGroup.length != GROUP_LENGTH) {
            return false;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ipGroup.length; i++) {
            if (ipGroup[i] < MIN_VALUE || ipGroup[i] > MAX_VALUE) {
                return false;
            }
            String binRepIP = getBinRepIP(ipGroup[i]);
            sb.append(binRepIP);
        }
        int zeroIndex = sb.length();
        for (int i = 0; i < sb.length(); i++) {
            char c = sb.charAt(i);
            if (c == '0') {
                zeroIndex = i;
            }

            if (i > zeroIndex) {
                if (sb.charAt(i) == '1') {
                    isOk = false;
                    break;
                }
            }
        }

        return isOk;
    }

    /**
     * IP地址是否有效
     *
     * @param ip IP地址
     * @return 是否有效
     */
    public boolean isValidIP(String ip) {
        int[] ipGroup = getIpGroup(ip);
        if (ipGroup == null || ipGroup.length != GROUP_LENGTH) {
            return false;
        }
        for (int i = 0; i < ipGroup.length; i++) {
            if (ipGroup[i] < MIN_VALUE || ipGroup[i] > MAX_VALUE) {
                return false;
            }
        }
        if (ipGroup[0] == 127) {
            return false;
        }
        return true;
    }

    /**
     * 获取子网IP
     *
     * @param ip     IP地址
     * @param maskIP 子网掩码
     * @return 子网IP
     */
    public String getSubIP(String ip, String maskIP) {
        boolean validMaskIP = isValidMaskIP(maskIP);
        if (!validMaskIP) {
            return ERROR_IP;
        }
        boolean validIP = isValidIP(ip);
        if (!validIP) {
            return ERROR_IP;
        }
        int[] ipGroup = getIpGroup(ip);
        int[] maskIPGroup = getIpGroup(maskIP);
        int[] subNetIp = new int[ipGroup.length];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ipGroup.length; i++) {
            int andRes = ipGroup[i] & maskIPGroup[i];
            subNetIp[i] = andRes;
            sb.append(andRes);
            sb.append(".");
        }
        String res = sb.substring(0, sb.length() - 1);
        return res;
    }

    /**
     * 判断两个ip是否在同一个子网内
     *
     * @param ipSrc  源IP
     * @param ipDest 目标IP
     * @param mask   子网掩码
     * @return 是否在同一子网内
     */
    public boolean isInSameSubNet(String ipSrc, String ipDest, String mask) {
        String subIP1 = getSubIP(ipSrc, mask);
        String subIP2 = getSubIP(ipDest, mask);
        return subIP1.equals(subIP2);
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            IPHelper ipHelper = new IPHelper();
            String myIP = scanner.next();
            if (!ipHelper.isValidIP(myIP)) {
                System.out.println("Invalid IP address.");
                continue;
            }
            String maskIP = scanner.next();
            if (!ipHelper.isValidMaskIP(maskIP)) {
                System.out.println("Invalid netmask address.");
                continue;
            }
            int n = scanner.nextInt();
            for (int i = 0; i < n; i++) {
                String oIP = scanner.next();
                if (!ipHelper.isValidIP(oIP)) {
                    System.out.println("Invalid IP address.");
                    continue;
                }
                boolean inSameSubNet = ipHelper.isInSameSubNet(myIP, oIP, maskIP);
                if (inSameSubNet) {
                    System.out.println("let's rock");
                } else {
                    System.out.println("not you");
                }
            }
//            String ip = scanner.nextLine();
//            int[] ipGroup = ipHelper.getIpGroup(ip);
//            for (int i = 0; i < ipGroup.length; i++) {
//                System.out.println(ipGroup[i]);
//                String binRepIP = ipHelper.getBinRepIP(ipGroup[i]);
//                System.out.println(binRepIP);
//
//            }
//            System.out.println(ip + ": mask valid:" + ipHelper.isValidMaskIP(ip));
//            System.out.println(ip + ": ip valid:" + ipHelper.isValidIP(ip));


//            int m = scanner.nextInt();
//            System.out.println(Integer.toBinaryString(m));
//            int n = scanner.nextInt();
//            System.out.println(Integer.toBinaryString(n));
//            int r = m & n;
//            System.out.println(r);
//            System.out.println(Integer.toBinaryString(r));
//            String strBin = Integer.toBinaryString(scanner.nextInt());
//            System.out.println(String.format("%08d", strBin));
        }

    }
}
